package com.material.common.utils.poi;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ganxinge
 */
public class ExcelTemplateUtil {

    public static void getTemplate(HttpServletResponse response, String sheetName,List<String> headers){
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet(sheetName);
        Row headerRow = sheet.createRow(0);

        // 创建表头单元格内容
        //String[] headers = {"供应商", "燃料品名", "车牌号", "水份(%)", "灰份(%)", "挥发份(%)", "含硫(%)", "热值(kCal/kg)"};
        for (int i = 0; i < headers.size(); i++) {
            Cell cell = headerRow.createCell(i);
            String s = headers.get(i);
            cell.setCellValue(s);
        }

        setStyle(workbook,sheet,headerRow,headers);
        // 设置表头行高为 30 磅
        headerRow.setHeight((short) (20 * 30));

        try {
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }finally
        {
            IOUtils.closeQuietly(workbook);
        }

    }

    private static void setStyle(Workbook workbook, Sheet sheet, Row headerRow, List<String> headers){
        // 创建表头样式
        CellStyle headerStyle = workbook.createCellStyle();

        // 设置字体样式
        Font font = workbook.createFont();
        // 加粗
        font.setBold(true);
        // 字体大小
        font.setFontHeightInPoints((short) 12);
        // 字体颜色为白色
        font.setColor(IndexedColors.BLACK.getIndex());
        headerStyle.setFont(font);

        // 设置背景颜色
        headerStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 设置边框
        headerStyle.setBorderTop(BorderStyle.THIN);
        headerStyle.setBorderBottom(BorderStyle.THIN);
        headerStyle.setBorderLeft(BorderStyle.THIN);
        headerStyle.setBorderRight(BorderStyle.THIN);

        // 设置水平和垂直对齐方式
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 应用表头样式
        for (int i = 0; i < headers.size(); i++) {
            Cell cell = headerRow.getCell(i);
            cell.setCellStyle(headerStyle);
            sheet.setColumnWidth(i, 256 * 20);
        }
    }
}
